package com.wnxy.web.servlet;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnxy.entity.Employee;
import com.wnxy.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/onePage")
public class OnePageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//处理接收POST请求的乱码
        resp.setContentType("text/html;charset=UTF-8");//解决响应到浏览器的内容的乱码问题
        //1. 获取请求参数： 当前页码
        String cp = req.getParameter("currentPage");
        int currentPage = 1;
        if(cp != null && !"".equals(cp)){
            currentPage = Integer.parseInt(cp);
        }
        //处理多组合的查询
        Employee emp = new Employee();
        String idStr = req.getParameter("id");
        if(idStr != null && !"".equals(idStr)){
            emp.setId(Integer.parseInt(idStr));
        }
        emp.setName(req.getParameter("ename"));
        emp.setTno(req.getParameter("tno"));
        emp.setTitle(req.getParameter("title"));
        String salaryStr = req.getParameter("salary");
        if(salaryStr != null && !"".equals(salaryStr)){
            emp.setSalary(new BigDecimal(salaryStr));
        }
        EmployeeServiceImpl esi =new EmployeeServiceImpl();
        PageInfo<Employee> info = esi.findOnePage(currentPage,emp);
        //使用分页插件
        List<Employee> list = esi.findAllemp();
        if (list!=null){
            //4. 向request域中存储数据
//            System.out.println(info);
            req.setAttribute("info", info);
//            System.out.println(emp);
            req.setAttribute("emp",emp);
        }
        //2. 转发到index.jsp
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
